package fi.piet.ec2.department;

import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.FormComponentFeedbackBorder;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.validation.validator.StringValidator;

import fi.piet.ec2.service.DepartmentService;

public class DepartmentForm extends Form {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@SpringBean
	private DepartmentService departmentService;
	
	public DepartmentForm(final String id, final Department department) {
		super(id, new CompoundPropertyModel(department));
        
		TextField name = new TextField("name");
		name.setRequired(true);
		name.add(StringValidator.maximumLength(24));
		name.add(StringValidator.minimumLength(3));
        
        FormComponentFeedbackBorder nameFeedback = new FormComponentFeedbackBorder("nameFeedback");
        add(nameFeedback);
        nameFeedback.add(name);
        
		TextField description = new TextField("description");
        
        FormComponentFeedbackBorder descriptionFeedback = new FormComponentFeedbackBorder("descriptionFeedback");
        add(descriptionFeedback);
        descriptionFeedback.add(description);
	}
	
	public void onSubmit() {
		// final RequestCycle cycle = getRequestCycle();
		// PageParameters parameters = new PageParameters();
		Department dep = (Department)getModelObject();
		departmentService.storeDepartment(dep);
		System.out.println("Submitting department, name: " + dep.getName() + ", desc: " + dep.getDescription());
		setResponsePage(DepartmentPage.class);
	}
}
